/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * GenerateHealthCriteria.java
 *
 * Created on 12.01.2012, 22:26:10
 */
package promogui;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author Friedemann Metzger
 */
public class GenerateHealthCriteria extends javax.swing.JFrame {

    /** Creates new form GenerateHealthCriteria */
    public GenerateHealthCriteria(PromoGUIMain main) {
        initComponents();
        this.main = main;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        buttonGroup1 = new javax.swing.ButtonGroup();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        zaehler = new javax.swing.JComboBox();
        nenner = new javax.swing.JComboBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        jSeparator3 = new javax.swing.JSeparator();
        criteriaName = new java.awt.TextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        save = new javax.swing.JButton();
        cancel = new javax.swing.JButton();
        faktor = new javax.swing.JComboBox();
        value1 = new javax.swing.JComboBox();
        value2 = new javax.swing.JComboBox();
        opt1 = new javax.swing.JCheckBox();
        opt2 = new javax.swing.JCheckBox();

        setTitle("Gesundheitskriterium anlegen");
        setResizable(false);

        jLabel2.setText("_____________________");

        jLabel3.setText("x");

        zaehler.setModel(

            new javax.swing.DefaultComboBoxModel(

                AttributeChooser.getParameterList().toArray()

            ));
            zaehler.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    zaehlerActionPerformed(evt);
                }
            });

            nenner.setModel(new javax.swing.DefaultComboBoxModel(AttributeChooser.getParameterList().toArray()));

            jLabel4.setText("=  X");

            jLabel5.setText("Projekt ist krank wenn....");

            criteriaName.setText("");
            criteriaName.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    criteriaNameActionPerformed(evt);
                }
            });

            jLabel7.setText("Name für das Gesundheitskriterium:");

            jLabel8.setText("Berechnung des Ausgangswertes....");

            save.setText("Speichern");
            save.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    saveActionPerformed(evt);
                }
            });

            cancel.setText("Abbrechen");
            cancel.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    cancelActionPerformed(evt);
                }
            });

            faktor.setModel(new javax.swing.DefaultComboBoxModel(AttributeChooser.getParameterList().toArray()
            ));

            value1.setEditable(true);
            value1.setModel(new javax.swing.DefaultComboBoxModel(AttributeChooser.getParameterList().toArray()));

            value2.setEditable(true);
            value2.setModel(new javax.swing.DefaultComboBoxModel(AttributeChooser.getParameterList().toArray()));

            opt1.setText(" X <=");

            opt2.setText(" X >=");
            opt2.addActionListener(new java.awt.event.ActionListener() {
                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    opt2ActionPerformed(evt);
                }
            });

            javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
            getContentPane().setLayout(layout);
            layout.setHorizontalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel7, javax.swing.GroupLayout.DEFAULT_SIZE, 178, Short.MAX_VALUE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(criteriaName, javax.swing.GroupLayout.DEFAULT_SIZE, 173, Short.MAX_VALUE)
                            .addGap(22, 22, 22))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel8, javax.swing.GroupLayout.DEFAULT_SIZE, 212, Short.MAX_VALUE)
                            .addGap(171, 171, 171))
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel5, javax.swing.GroupLayout.DEFAULT_SIZE, 158, Short.MAX_VALUE)
                            .addGap(225, 225, 225))
                        .addComponent(jSeparator1, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE)
                        .addComponent(jSeparator3, javax.swing.GroupLayout.DEFAULT_SIZE, 383, Short.MAX_VALUE))
                    .addContainerGap())
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                    .addGap(25, 25, 25)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(nenner, javax.swing.GroupLayout.Alignment.LEADING, 0, 156, Short.MAX_VALUE)
                        .addComponent(zaehler, 0, 156, Short.MAX_VALUE)
                        .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 156, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(18, 18, 18)
                    .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 14, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(faktor, 0, 125, Short.MAX_VALUE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, 23, Short.MAX_VALUE)
                    .addGap(28, 28, 28))
                .addGroup(layout.createSequentialGroup()
                    .addGap(81, 81, 81)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(cancel, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 28, Short.MAX_VALUE)
                            .addComponent(save, javax.swing.GroupLayout.PREFERRED_SIZE, 92, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(opt1)
                                .addComponent(opt2))
                            .addGap(18, 18, 18)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(value2, javax.swing.GroupLayout.Alignment.TRAILING, 0, 148, Short.MAX_VALUE)
                                .addComponent(value1, javax.swing.GroupLayout.Alignment.TRAILING, 0, 148, Short.MAX_VALUE))))
                    .addGap(103, 103, 103))
            );
            layout.setVerticalGroup(
                layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(layout.createSequentialGroup()
                    .addGap(36, 36, 36)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel7)
                        .addComponent(criteriaName, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGap(24, 24, 24)
                    .addComponent(jSeparator3, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(jLabel8)
                    .addGap(18, 18, 18)
                    .addComponent(zaehler, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel2)
                        .addComponent(jLabel3)
                        .addComponent(faktor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jLabel4))
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(nenner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(17, 17, 17)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jLabel5)
                    .addGap(18, 18, 18)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(value1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(opt1))
                    .addGap(9, 9, 9)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(value2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(opt2))
                    .addGap(46, 46, 46)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cancel)
                        .addComponent(save, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGap(20, 20, 20))
            );

            pack();
        }// </editor-fold>//GEN-END:initComponents

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
    this.setVisible(false);// TODO add your handling code here:
}//GEN-LAST:event_cancelActionPerformed

private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
            
            String name = criteriaName.getText();
                
            if (name.equals(""))

            {
                JOptionPane.showMessageDialog(null, "..Sie haben vergessen dem Kriterium einen Namen zu geben.", "Hoppla..", JOptionPane.ERROR_MESSAGE);
                return;
            }
            double val1, val2;
            val1= -1;
            val2= -1;
            boolean val1_isCustom = false, val2_isCustom = false;
            int opt = -1 ;
            if (opt1.isSelected() && !opt2.isSelected())
            {
                opt = 0;
            }
            
            if (!opt1.isSelected() && opt2.isSelected())
            {
                opt = 1;
            }
            
            if (opt1.isSelected() && opt2.isSelected())
            {
                opt = 2;
            }
            
            if (opt == -1)
            {
                JOptionPane.showMessageDialog(null, "..Sie haben vergessen einen Schwellwert zu definieren", "Hoppla..", JOptionPane.ERROR_MESSAGE);
            }
            else
            {

                if ( value1.getSelectedIndex() == -1)
                {
                    
                    String value = value1.getEditor().getItem().toString();
                    if (value.matches("\\d+|\\d+.\\d+"))
                    {
                    val1 = Double.parseDouble(value);
                   
                    val1_isCustom = true;
                    }
                    else
                    {
                         JOptionPane.showMessageDialog(null, "..Sie dürfen nur nummerische Werte verwenden.", "Hoppla..", JOptionPane.ERROR_MESSAGE);
                         return;
                    }
                }
                if ( value2.getSelectedIndex() == -1)
                {
                    String value = value2.getEditor().getItem().toString();
                    if (value.matches("\\d+|\\d+.\\d+"))
                    {
                    val2 = Double.parseDouble(value);
                   
                    val2_isCustom = true;
                    }
                    else
                    {
                         JOptionPane.showMessageDialog(null, "..Sie dürfen nur nummerische Werte verwenden.", "Hoppla..", JOptionPane.ERROR_MESSAGE);
                         return;
                    }
                }

                //System.out.println(val1);
                
                

                
                HealthCriteria criteria = new HealthCriteria(zaehler.getSelectedIndex(), nenner.getSelectedIndex(), faktor.getSelectedIndex(),opt ,val1_isCustom, val1, val2_isCustom, val2, name );

               
                //------------------------- <TEST> ----------------------
            // try {
                
//                Project proj = CSVIO.getProjects().get(0);
//
//
//                ArrayList<Double> values = criteria.calc(proj);
//                Iterator<Double> it = values.iterator();
//                while (it.hasNext())
//                {
//                    System.out.println(it.next().doubleValue());
//                }
//
//                ArrayList<Boolean> values2 = criteria.isFit(proj);
//
//                Iterator<Boolean> it2 = values2.iterator();
//                while (it2.hasNext())
//                {
//                    System.out.println("ungesund? "+it2.next());
//                }
//
//
//
//                //------------------------- </TEST> ----------------------
//
//                // TODO add your handling code here:
//            } catch (FileNotFoundException ex) {
//                Logger.getLogger(GenerateHealthCriteria.class.getName()).log(Level.SEVERE, null, ex);
//            } catch (IOException ex) {
//                Logger.getLogger(GenerateHealthCriteria.class.getName()).log(Level.SEVERE, null, ex);
//            }
                
           main.addCriteria(criteria);
 }
}//GEN-LAST:event_saveActionPerformed

private void opt2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opt2ActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_opt2ActionPerformed

private void zaehlerActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_zaehlerActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_zaehlerActionPerformed

private void criteriaNameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_criteriaNameActionPerformed
// TODO add your handling code here:
}//GEN-LAST:event_criteriaNameActionPerformed




    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GenerateHealthCriteria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GenerateHealthCriteria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GenerateHealthCriteria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GenerateHealthCriteria.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                //new GenerateHealthCriteria().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.ButtonGroup buttonGroup1;
    private javax.swing.JButton cancel;
    private java.awt.TextField criteriaName;
    private javax.swing.JComboBox faktor;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator3;
    private javax.swing.JComboBox nenner;
    private javax.swing.JCheckBox opt1;
    private javax.swing.JCheckBox opt2;
    private javax.swing.JButton save;
    private javax.swing.JComboBox value1;
    private javax.swing.JComboBox value2;
    private javax.swing.JComboBox zaehler;
    // End of variables declaration//GEN-END:variables
    private PromoGUIMain main;
}
